﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient; 


namespace RGPWEB.admin.Hjbom
{
    public partial class ProductSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                this.bind();

                TextBox1.Attributes.Add("onkeydown", "if(event.keyCode==13)  {document.all." + BtnSearch.ClientID + ".focus();document.all." + this.BtnSearch.ClientID + ".click();}");


            }


            //this.BtnSearch_Click(sender,null);

          //  this.bind();


        }

        protected void bind()
        {
            

            SqlConnection oConn = new SqlConnection(SDS_Search.ConnectionString);
            SqlDataReader oDr;
            try
            {
                oConn.Open();
                SqlCommand oCmd = new SqlCommand("SELECT FNumber, FName, FItemID, FItemClassID, FParentID, FLevel, FDeleted FROM t_Item WHERE ((FItemClassID = 4)And(FDeleted <>1)And(Flevel<3)) ORDER BY FNumber", oConn);
                oDr = oCmd.ExecuteReader();
                //定义对象数组
                TreeNode[] myNodes = new TreeNode[100000];
                //初始化对象数组
                for (int i = 0; i < myNodes.Length; i++)
                {
                    myNodes[i] = new TreeNode();
                }
                //从表中取数据
                while (oDr.Read())
                {
                    int iParentNodeId = (int)oDr["FParentID"];

                    int iMyNodeId = (int)oDr["FItemID"];

                    if (iParentNodeId != 0)
                    {
                        myNodes[iMyNodeId].Text = oDr["Fnumber"].ToString()+"["+oDr["FName"].ToString()+"]";
                        myNodes[iMyNodeId].Value = oDr["Fnumber"].ToString();


                      //  myNodes[iMyNodeId]. = oDr["FName"].ToString();

                      //  myNodes[iMyNodeId].NavigateUrl = oDr["testpath"].ToString();

                        myNodes[iParentNodeId].ChildNodes.Add(myNodes[iMyNodeId]);
                    }
                    else
                    {
                        myNodes[iMyNodeId].Text = oDr["Fnumber"].ToString() + "[" + oDr["FName"].ToString() + "]";
                        myNodes[iMyNodeId].Value = oDr["Fnumber"].ToString();

                        myNodes[iMyNodeId].SelectAction = TreeNodeSelectAction.None;

                        TreeView1.Nodes.Add(myNodes[iMyNodeId]);
                    }
                }
            }
            catch (System.Exception sqle)
            {
                sqle.ToString().Replace("\n", "<br>");

                Response.Write(sqle);
            }
            finally
            {
                oConn.Close();
            }
        }




        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TextBox1.Text = TreeView1.SelectedNode.Value;
     
            RbtnSelect.SelectedIndex = 3;

            this.BtnSearch_Click(sender, null);

        }

        protected void GV_Search_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {

            


            //TextBox1.Text = "---";

            string strNumberSelect = " SELECT  [FNumber] As [料号],[FName] As [名称], [FModel] As [规格], [FNote] As [备注] FROM [t_ICItem] WHERE ([FNumber] LIKE '%'+@fnumber_t+'%') AND ([FDeleted] <> 1)";
            string strNameSelect = " SELECT  [FNumber] As [料号],[FName] As [名称], [FModel] As [规格], [FNote] As [备注] FROM [t_ICItem] WHERE ([FName] LIKE '%'+@fname_t+'%') AND ([FDeleted] <> 1)";
            string strModelSelect = " SELECT  [FNumber] As [料号],[FName] As [名称], [FModel] As [规格], [FNote] As [备注] FROM [t_ICItem] WHERE ([FModel] LIKE '%'+@fmodel_t+'%') AND ([FDeleted] <> 1)";

            string strNumberSelectedNode = " SELECT  [FNumber] As [料号],[FName] As [名称], [FModel] As [规格], [FNote] As [备注] FROM [t_ICItem] WHERE ([FNumber] LIKE @fnumber_t+'%') AND ([FDeleted] <> 1)";
            
            string str = "";
          
            switch (RbtnSelect.SelectedIndex)
            {
                case 0: str = strNumberSelect; break;
                case 1: str = strNameSelect; break;
                case 2: str = strModelSelect; break;
                case 3: str = strNumberSelectedNode; break;
            }

         
            

            SDS_GridView.SelectParameters.Clear();

            SDS_GridView.ConnectionString = SDS_Search.ConnectionString;



            SDS_GridView.SelectCommand = str;

         //   SqlParameter[] parameters = {
         //            new SqlParameter("@fnumber_t", SqlDbType.NVarChar,128),
		//			 new SqlParameter("@fname_t", SqlDbType.NVarChar,128)};
         //   parameters[0].Value = TextBox1.Text;
          //  parameters[1].Value = TextBox1.Text;


        SDS_GridView.SelectParameters.Add("fnumber_t", TextBox1.Text);
        SDS_GridView.SelectParameters.Add("fname_t", TextBox1.Text);
        SDS_GridView.SelectParameters.Add("fmodel_t", TextBox1.Text);

            //SDS_GridView.FilterParameters.Add("fnumber_text",SqlDbType.VarChar,50) = TextBox1.Text;

            //SDS_GridView.InsertParameters["fnumber_text"].DefaultValue = TextBox1.Text;


           // SDS_GridView.DataSourceMode = SqlDataSourceMode.DataSet;


           


            GV_Search.DataSourceID = SDS_GridView.ID;

            
          
            



          // GV_Search.DataBind();
            

            
            /*



            TextBox1.Text = "1";

            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = SDS_GridView.ConnectionString;

            conn.Open();

            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn;

            cmd.CommandText = " SELECT FNumber, FName, FModel, FNote, FDeleted FROM t_ICItem WHERE (FNumber LIKE '%'+@id+'%') AND (FDeleted <> 1)";

            Label1.Text = cmd.CommandText;

            cmd.Parameters.Add("@id", SqlDbType.VarChar, 30);

            cmd.Parameters["@id"].Value = Convert.ToString(TextBox1.Text);

            cmd.ExecuteNonQuery();
            
            SqlDataAdapter dr = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            dr.Fill(ds, "fname");
            this.GV_Search.DataSource = ds.Tables["fname"];
            this.GV_Search.DataBind();

            conn.Close();
           
            
            //cmd.Connection = conn;

            SqlDataReader dr;

            //cmd.Connection = conn;


            dr = cmd.ExecuteReader();

            GV_Search.DataSource = dr;

            GV_Search.DataBind();

            conn.Close();
            */


       
            
            
            
            
            
            
            
            
            //SDS_GridView.SelectCommand = " SELECT * FROM [authors]  WHERE ([au_lname] LIKE '%' + @au_lname + '%')";

            //  this.SDS_GridView.SelectCommand = "SELECT * FROM [t_ICitem] where [FNumber] = " +"\""+ DDL_Search.Text.ToString()+"\"";
           //   this.GV_Search.DataSourceID = "SDS_GridView";
           //   this.GV_Search.DataBind();

        }

       //  向SqlDataSource中添加四个Parameter
    protected void AddSelectParameters()
    {
        //清空所有Parameter
        this.SDS_GridView.SelectParameters.Clear();

        //加载四个Parameter
        SessionParameter UserInfoString = new SessionParameter("UserInfoString", "UserInfoString");
        this.SDS_GridView.SelectParameters.Add(UserInfoString);

        ControlParameter BeginDate = new ControlParameter();
        BeginDate.Name = "BeginDate";
        BeginDate.Type = TypeCode.DateTime;
        BeginDate.ControlID = "BeginDate";
        BeginDate.PropertyName = "SelectedDate";
        this.SDS_GridView.SelectParameters.Add(BeginDate);

        ControlParameter EndDate = new ControlParameter();
        EndDate.Name = "EndDate";
        EndDate.Type = TypeCode.DateTime;
        EndDate.ControlID = "EndDate";
        EndDate.PropertyName = "SelectedDate";
        this.SDS_GridView.SelectParameters.Add(EndDate);

        Parameter ErrorMessage = new Parameter();
        ErrorMessage.Name = "ErrorMessage";
        ErrorMessage.DefaultValue = "";
        this.SDS_GridView.SelectParameters.Add(ErrorMessage);
        //加载Parameter结束
    }

    protected void GV_Search_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }
    }

    protected void GV_Search_SelectedIndexChanged1(object sender, EventArgs e)
    {
        //

        //Response.Write("<scrtip>window.open('mm_detailsview.aspx')</script>");
        



    }



 


        //绑定数据方法
       // private void DGBind(){  
            //你的Sql语句 
         //   string strSql = "select * from tablename where ....";     
            //绑定dataset ，可以用微软的SqlHelper类 
         //   DataSet objDs = SqlHelper.Query(strSql);

         //   GV_Search.DataSource = dataset.tables[0].defaultview;
         //   GV_Search.pageindex = pageindex;
         //   GV_Search.databind();


         //   GV_Search.DataSource = objDs.Tables[0].DefaultView;
         //   GV_Search.DataBind();
       // }


    }
}
